import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);  //使用路由


const routes = [
    {
        path:"/",
        redirect:{name:'home'},
        component:() => import("@/views/layout/index.vue"),
        children:[
            {
                path:'home',
                name:'home',
                component:() => import("@/views/layout/Home.vue")
            },
            {
                path:"art-cate",
                component:() => import("@/views/layout/ArtCate.vue")
            },
            {
                path:"art-list",
                name:"artlist",
                component:() => import("@/views/layout/ArtList.vue")
            },
            {
                path:"user-info",
                name:"userinfo",
                component:() => import("@/views/layout/UserInfo.vue")
            },
            {
                path:"user-avatar",
                name:"useravatar",
                component:() => import("@/views/layout/UserAvatar.vue")
            },
            {
                path:"user-pwd",
                name:"userpwd",
                component:() => import("@/views/layout/UserPwd.vue")
            }
        ] 
    },
    {
        path:"/login",
        name:"login",
        component:() => import("@/views/Login.vue")
    },
    {
        path:"/register",
        name:"register",
        component:() => import("@/views/Register.vue")
    }
];

const router = new VueRouter({
    routes
})
//全局前置路由守卫
router.beforeEach((to,from,next)=>{
    let token = localStorage.getItem("user_token");
    if(to.path == '/login'|| to.path =='/register'){
        next();
    }else{
        if(token != null){
            next();
        }else{
            next("/login");
        }
    }
})

export default router;